package com.android.base.tools;

import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.lang.reflect.Field;

public class EditTextUtil {
	/**
	 * 检测edittext内容是否空
	 * @param et
	 * @return
	 */
	public static boolean isEmpty(EditText et){
		if(null == et) return false;
		if(null != et && !et.getText().toString().trim().equals("")){
			//不为空
			return false;
		}
		return true;
	}

	/**
	 * 禁止输入框复制粘贴菜单
	 */
	public static void disableCopyAndPaste(final EditText editText) {
		try {
			if (editText == null) {
				return ;
			}

			editText.setOnLongClickListener(new View.OnLongClickListener() {
				@Override
				public boolean onLongClick(View v) {
					return true;
				}
			});
			editText.setLongClickable(false);
			editText.setOnTouchListener(new View.OnTouchListener() {
				@Override
				public boolean onTouch(View v, MotionEvent event) {
					if (event.getAction() == MotionEvent.ACTION_DOWN) {
						// setInsertionDisabled when user touches the view
						setInsertionDisabled(editText);
					}

					return false;
				}
			});
			editText.setCustomSelectionActionModeCallback(new ActionMode.Callback() {
				@Override
				public boolean onCreateActionMode(ActionMode mode, Menu menu) {
					return false;
				}

				@Override
				public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
					return false;
				}

				@Override
				public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
					return false;
				}

				@Override
				public void onDestroyActionMode(ActionMode mode) {

				}
			});
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void setInsertionDisabled(EditText editText) {
		try {
			Field editorField = TextView.class.getDeclaredField("mEditor");
			editorField.setAccessible(true);
			Object editorObject = editorField.get(editText);

			// if this view supports insertion handles
			Class editorClass = Class.forName("android.widget.Editor");
			Field mInsertionControllerEnabledField = editorClass.getDeclaredField("mInsertionControllerEnabled");
			mInsertionControllerEnabledField.setAccessible(true);
			mInsertionControllerEnabledField.set(editorObject, false);

			// if this view supports selection handles
			Field mSelectionControllerEnabledField = editorClass.getDeclaredField("mSelectionControllerEnabled");
			mSelectionControllerEnabledField.setAccessible(true);
			mSelectionControllerEnabledField.set(editorObject, false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
